package com.china08.yxyapi.api.manage.tec.teaching.continuedu.mytask;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTask;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskRepository;

/**
 * 
 * @annotation: 继续教育-判断是否是自己正在进行中的任务
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyMyTaskStatusSchController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduUserTaskRepository yxyContinueduUserTaskRepository;

    @Autowired
    private YxyContinueduTaskRepository yxyContinueduTaskRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/mytask/status/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();

        String taskId = reqModel.taskId;

        RespModel model = new RespModel();

        YxyContinueduTask yxyContinueduTask = yxyContinueduTaskRepository.findById(taskId);
        model.taskName = yxyContinueduTask.getName();
        YxyContinueduUserTask userTask = yxyContinueduUserTaskRepository.findByUserIdAndTaskIdAndStatus(userId, taskId,
                0);

        if (userTask == null) {
            model.task = false;
        } else {
            model.task = true;
        }
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 任务ID
        public String taskId;
    }

    protected static class RespModel extends YxyApiRespModel {

        public boolean task;

        public String taskName;
    }
}
